package com.ihr360.payroll.service.payroll.convertor;

import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.config.SalaryFieldCategoryEntity;
import com.ihr360.payroll.config.SalaryFieldTypeEntity;
import com.ihr360.payroll.config.SalaryTaxTypeEntity;
import com.ihr360.payroll.config.SystemSalaryFieldValueEntity;
import com.ihr360.payroll.config.system.SystemSalaryFieldEntity;
import com.ihr360.payroll.dao.SalaryTaxTypeRepository;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryFieldCategory;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryFieldType;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryTaxType;
import com.ihr360.payroll.model.dto.salary.embeddable.SystemSalaryFieldValue;
import com.ihr360.payroll.model.dto.salary.meta.system.SystemSalaryField;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SystemSalaryFieldConvertor implements IConvertor<SystemSalaryFieldEntity, SystemSalaryField> {

    @Autowired
    private SalaryTaxTypeRepository salaryTaxTypeRepository;

    @Override
    public SystemSalaryField convertEntityToDto(SystemSalaryFieldEntity entity) {
        if (entity == null) {
            return null;
        }
        SystemSalaryField systemSalaryField = new SystemSalaryField();
        BeanUtils.copyProperties(entity, systemSalaryField);

        SystemSalaryFieldValueEntity valueEntity = entity.getValueMeta();
        if (valueEntity != null) {
            SystemSalaryFieldValue systemSalaryFieldValue = new SystemSalaryFieldValue();
            BeanUtils.copyProperties(valueEntity, systemSalaryFieldValue);
            systemSalaryField.setValueMeta(systemSalaryFieldValue);
        }
        SalaryTaxTypeEntity taxTypeEntity = entity.getTaxType();
        if (taxTypeEntity != null && taxTypeEntity.getId() != null) {
            SalaryTaxType salaryTaxType = new SalaryTaxType();
            salaryTaxType.setId(taxTypeEntity.getId());
            systemSalaryField.setTaxType(salaryTaxType);
        }
        return systemSalaryField;
    }

    @Override
    public SystemSalaryFieldEntity convertDtoToEntity(SystemSalaryField dto) {
        if (dto == null) {
            return null;
        }

        SystemSalaryFieldEntity entity = new SystemSalaryFieldEntity();
        BeanUtils.copyProperties(dto, entity, "category", "type", "refProperty", "taxType");
        SystemSalaryFieldValue fieldValue = dto.getValueMeta();
        if (fieldValue != null) {
            SystemSalaryFieldValueEntity fieldValueEntity = new SystemSalaryFieldValueEntity();
            BeanUtils.copyProperties(fieldValue, fieldValueEntity);
            entity.setValueMeta(fieldValueEntity);
        }

        SalaryFieldType fieldType = dto.getType();
        if (fieldType != null) {
            SalaryFieldTypeEntity fieldTypeEntity = new SalaryFieldTypeEntity();
            BeanUtils.copyProperties(fieldType, fieldTypeEntity);
            entity.setType(fieldTypeEntity);
            List<SalaryTaxType> salaryTaxTypes = fieldType.getSalaryTaxTypes();
            if (CollectionUtils.isNotEmpty(salaryTaxTypes)) {
                List<SalaryTaxTypeEntity> salaryTaxTypeEntities = Lists.newArrayListWithCapacity(salaryTaxTypes.size());
                salaryTaxTypes.forEach(salaryTaxType -> {
                    SalaryTaxTypeEntity salaryTaxTypeEntity = new SalaryTaxTypeEntity();
                    BeanUtils.copyProperties(salaryTaxType, salaryTaxTypeEntity);
                    salaryTaxTypeEntities.add(salaryTaxTypeEntity);
                });
                fieldTypeEntity.setSalaryTaxTypes(salaryTaxTypeEntities);
            }
        }

        SalaryTaxType taxType = dto.getTaxType();
        if (taxType != null) {
            SalaryTaxTypeEntity taxTypeEntity = new SalaryTaxTypeEntity();
            BeanUtils.copyProperties(taxType, taxTypeEntity);
            entity.setTaxType(taxTypeEntity);
        }
        SalaryFieldCategory category = dto.getCategory();
        if (category != null) {
            SalaryFieldCategoryEntity categoryEntity = new SalaryFieldCategoryEntity();
            BeanUtils.copyProperties(category, categoryEntity);
            entity.setCategory(categoryEntity);
        }
        return entity;
    }

    public List<SystemSalaryFieldEntity> convertDtosToEntities(List<SystemSalaryField> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return Lists.newArrayList();
        }
        List<SystemSalaryFieldEntity> systemSalaryFieldEntities = Lists.newArrayListWithCapacity(dtos.size());
        dtos.forEach(dto -> {
            systemSalaryFieldEntities.add(convertDtoToEntity(dto));
        });
        return systemSalaryFieldEntities;
    }

}